package com.noah.note.presenter;

import com.google.gson.Gson;
import com.noah.note.R;
import com.noah.note.bean.Reply;
import com.noah.note.bean.ReplyComment;
import com.noah.note.bean.User;
import com.noah.note.global.BasePresenter;
import com.noah.note.global.Const;
import com.noah.note.global.OkHttpCallback;
import com.noah.note.manager.JsonManager;
import com.noah.note.manager.LoginManager;
import com.noah.note.model.LoginModel;
import com.noah.note.utils.LogUtils;
import com.noah.note.utils.NetUtils;
import com.noah.note.utils.SPUtils;
import com.noah.note.utils.ToastUtils;
import com.noah.note.view.LoginView;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class LoginPresenter extends BasePresenter<LoginView> {
    LoginModel model;

    public LoginPresenter() {
        model = new LoginModel();
    }

    public void login(final String username, final String password) {
        if (!NetUtils.isConnected()) {//无网络
            ToastUtils.show(R.string.failure_network);
            return;
        }
        if (getView() != null)
            getView().showDialog();
        model.login(username, password, new OkHttpCallback() {
            @Override
            public void onFail(Reply reply) {
                if (getView() != null)
                    getView().dismissDialog();
                switch (reply.getCode()){
                    case Const.ERROR_USERNAME:
                        ToastUtils.show(R.string.error_login_username);
                        break;
                    case Const.ERROR_PASSWORD:
                        ToastUtils.show(R.string.error_login_password);
                        break;
                    case Const.FAIL:
                        ToastUtils.show(R.string.failure_login);
                        break;
                    case Const.FAIL_REQUEST:
                        ToastUtils.show(R.string.failure_request);
                        break;
                }
            }

            @Override
            public void onSuccess(Reply reply) {
                if (getView() != null)
                    getView().dismissDialog();
                //用户资料
                User user = new Gson().fromJson(reply.getData(), User.class);
                LoginManager.getInstance().setUser(user);
                SPUtils.save(Const.SP_TOKEN, user.getToken());
                if (getView() != null) {
                    getView().loginSuccess();
                }
                ToastUtils.show(R.string.success_login);
            }
        });
    }

    public void login(final String token) {
        if (!NetUtils.isConnected()) {//无网络
            getView().loginFail();
            return;
        }
        model.login(token, new OkHttpCallback() {
            @Override
            public void onFail(Reply reply) {
                switch (reply.getCode()){
                    case Const.FAIL:
                        getView().loginFail();
                        break;
                    case Const.FAIL_REQUEST:
                        getView().loginFail();
                        break;
                    case Const.FAIL_TOKEN:
                        SPUtils.save(Const.SP_TOKEN, "");
                        getView().loginFail();
                        break;
                }
            }

            @Override
            public void onSuccess(Reply reply) {
                //用户资料
                User user = new Gson().fromJson(reply.getData(), User.class);
                LoginManager.getInstance().setUser(user);
                SPUtils.save(Const.SP_TOKEN, user.getToken());
                if (getView() != null) {
                    getView().loginSuccess();
                }
                ToastUtils.show(R.string.success_login);
            }
        });
    }

    public void register(String username, String password, String name) {
        if (!NetUtils.isConnected()) {//无网络
            ToastUtils.show(R.string.failure_network);
            return;
        }
        model.register(username, password, name, new OkHttpCallback() {
            @Override
            public void onFail(Reply reply) {
                switch (reply.getCode()){
                    case Const.ERROR_ALREADY:
                        ToastUtils.show(R.string.error_register_username);
                        break;
                    case Const.FAIL:
                        ToastUtils.show(R.string.failure_login);
                        break;
                    case Const.FAIL_REQUEST:
                        ToastUtils.show(R.string.failure_request);
                        break;
                }
            }

            @Override
            public void onSuccess(Reply reply) {
                if (getView() != null) {
                    getView().registerSuccess();
                }
                ToastUtils.show(R.string.success_register);
            }
        });
    }

    public void getCommentList(long nid, long time) {
        if (!NetUtils.isConnected()) {//无网络
            ToastUtils.show(R.string.failure_network);
            return;
        }
        model.getCommentList(nid, time, new OkHttpCallback() {
            @Override
            public void onFail(Reply reply) {
                ToastUtils.show(R.string.failure_gain);
            }

            @Override
            public void onSuccess(Reply reply) {
                int code = reply.getCode();
                ToastUtils.show(reply.getMsg());
                if (code == Const.SUCCESS) {//登录成功
                    String data = reply.getData();
                    ArrayList<ReplyComment> replyComments = (ArrayList<ReplyComment>) JsonManager.getInstance().toBeanList(data, ReplyComment.class);
                    LogUtils.i(replyComments.toString());
                }
            }
        });
    }
}
